package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.RecReceiveAdapter;
import com.wbao.dianniu.data.RecReceiveData;
import com.wbao.dianniu.listener.IJobCollectListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobCollectListManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 1.1.1.8	收藏记录列表
 * 
 * @author yejianjun
 *
 */
public class JobCollectActivity extends BaseActivity implements IJobCollectListListener {

	private JobCollectListManager manager;
	private PullToRefreshListView pullListView;
	private ListView listView;
	private int currentPage = 0;
	private List<RecReceiveData> mList = new ArrayList<RecReceiveData>();
	private RecReceiveAdapter adapter;
	private TextView noDataTV;
	private final int PAGE_COUNTS = 10;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_receive_resume,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.collection_record));
		initView();
		initData();
	}
	
	private void initView(){
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		pullListView = (PullToRefreshListView) findViewById(R.id.recResume_list);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		listView = pullListView.getRefreshableView();
		registerForContextMenu(listView);
	}
	
	private void initData(){
		manager = new JobCollectListManager(this);
		manager.addJobCollectListListener(this);
		adapter = new RecReceiveAdapter(this);
		adapter.addData(mList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(itemClickListener);
		reqData(0);
	}

	private void reqData(int page) {
		manager.jobCollectList(GlobalContext.getAccountId(),page * PAGE_COUNTS , PAGE_COUNTS);
	}
	
	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		
		@Override
		public void onRefresh() {
			//  上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	
	// 上拉刷新数据请求
	private void pullUpRequest(){
		//没有搜索结果 或者数据为空 上拉没有效果
		currentPage ++;
		reqData(currentPage);
	}
	
	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.putExtra(Const.INTENT_JOB_ID, mList.get(position-1).getId());
			intent.setClass(JobCollectActivity.this, JobDetailsActivity.class);
			startActivity(intent);
		}
	};

	@Override
	public void onJobCollectListFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		pullListView.onRefreshComplete(false, false);
		Notification.toast(this,errorMsg);
	}

	@Override
	public void onJobCollectListSuccess(List<RecReceiveData> list) {
		pullListView.onRefreshComplete(false, false);
		if(null != list){
			adapter.addData(list);
			adapter.notifyDataSetChanged();
			mList = adapter.getList();
			if(mList.size() == 0){
				noDataTV.setVisibility(View.VISIBLE);
			}else{
				noDataTV.setVisibility(View.GONE);
			}
		}
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(null != manager){
			manager.removeJobCollectListListener(this);
		}
	}
}
